package com.example.databasetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private MyDatabaseHelper dbHelper;

    @Override
    protected  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
        Button createDatabase = findViewById(R.id.create_database);
        createDatabase.setOnClickListener((v) -> {
            dbHelper.getWritableDatabase();
        });

        Button addData = findViewById(R.id.add_date);
        addData.setOnClickListener((v) -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            // 开始组装第一条数据
            values.put("name", "The Da Vinci Code");
            values.put("author", "Dan Brown");
            values.put("pages", 454);
            values.put("price", 16.96);
            db.insert("Book", null, values); // 插入第一条数据
            // 开始组装第二条数据
            values.put("name", "The Lost Symbol");
            values.put("author", "Dan Brown");
            values.put("pages", 989);
            values.put("price", 89.32);
            db.insert("Book", null, values); // 插入第二条数据
        });

        Button updateData = findViewById(R.id.update_data);
        updateData.setOnClickListener((v) -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("price", 10.32);
            db.update("Book", values, "name = ?", new String[] { "The Lost Symbol" });
        });

        Button deleteData = findViewById(R.id.delete_data);
        deleteData.setOnClickListener((v) -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.delete("Book", "pages > ?", new String[]{ "500" });
        });

        Button queryData = findViewById(R.id.query_data);
        queryData.setOnClickListener((v) -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            // 查询Book表中所有的数据
            Cursor cursor = db.query("Book", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    // 遍历cursor对象，取出数据并打印
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String author = cursor.getString(cursor.getColumnIndex("author"));
                    int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                    double price = cursor.getDouble(cursor.getColumnIndex("price"));

                    Log.d(TAG, "onCreate: name: " + name);
                    Log.d(TAG, "onCreate: author: " + author);
                    Log.d(TAG, "onCreate: pages: " + pages);
                    Log.d(TAG, "onCreate: price: " + price                                               );

                } while (cursor.moveToNext());
            }
            cursor.close();
        });
    }
}